﻿@page "/edit-trail/{TrailId:int}"
@inject IMediator Mediator

<PageTitle>Edit Trail - Blazing Trails</PageTitle>

<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="/">Home</a></li>
        <li class="breadcrumb-item active" aria-current="page">Edit Trail</li>
    </ol>
</nav>

@if (_isLoading)
{
    <p>Loading trail...</p>
}
else
{
    <h3 class="mt-5 mb-4">Editing trail: @_trail.Name</h3>

    @if (_submitSuccessful)
    {
        <SuccessAlert Message="Your trail has been edited successfully!" />
    }
    else if (_errorMessage is not null)
    {
        <ErrorAlert Message="@_errorMessage" />
    }

    <TrailForm Trail="_trail" OnSubmit="SubmitEditTrail" />
}

@code {
    private bool _isLoading;
    private bool _submitSuccessful;
    private string? _errorMessage;
    private TrailDto _trail = new TrailDto();

    [Parameter] public int TrailId { get; set; }

    protected override async Task OnInitializedAsync()
    {
        _isLoading = true;

        var response = await Mediator.Send(new GetTrailRequest(TrailId));

        if (response.Trail is not null)
        {
            _trail.Id = TrailId;
            _trail.Name = response.Trail.Name;
            _trail.Description = response.Trail.Description;
            _trail.Location = response.Trail.Location;
            _trail.Image = response.Trail.Image;
            _trail.Length = response.Trail.Length;
            _trail.TimeInMinutes = response.Trail.TimeInMinutes;
            _trail.Route.Clear();
            _trail.Route.AddRange(response.Trail.RouteInstructions.Select(ri => new TrailDto.RouteInstruction
            {
                Stage = ri.Stage,
                Description = ri.Description
            }));
        }
        else
        {
            _errorMessage = "There was a problem loading the trail.";
        }

        _isLoading = false;
    }

    private async Task SubmitEditTrail(TrailDto trail, IBrowserFile? image)
    {
        var response = await Mediator.Send(new EditTrailRequest(trail));
        if (!response.IsSuccess)
        {
            _submitSuccessful = false;
            _errorMessage = "There was a problem saving your trail.";
        }
        else
        {
            _trail.Name = trail.Name;
            _trail.Description = trail.Description;
            _trail.Location = trail.Location;
            _trail.Length = trail.Length;
            _trail.TimeInMinutes = trail.TimeInMinutes;
            _trail.Route.AddRange(trail.Route.Select(ri => new TrailDto.RouteInstruction
            {
                Stage = ri.Stage,
                Description = ri.Description
            }));

            _submitSuccessful = true;

            if (trail.ImageAction == ImageAction.Add) _submitSuccessful = await ProcessImage(trail.Id, image!);
            if (trail.ImageAction == ImageAction.Remove) _trail.Image = null;
        }

        StateHasChanged();
    }

    private async Task<bool> ProcessImage(int trailId, IBrowserFile trailImage)
    {
        var imageUploadResponse = await Mediator.Send(new UploadTrailImageRequest(trailId, trailImage));

        if (string.IsNullOrWhiteSpace(imageUploadResponse.ImageName))
        {
            _errorMessage = "Your trail was saved, but there was a problem uploading the image.";
            return false;
        }

        _trail.Image = imageUploadResponse.ImageName;
        return true;
    }
}
